﻿using System;
using System.Linq;
using System.Reflection;
using Volo.Abp;
using Volo.Abp.Localization;

namespace Joy.Erp.BasicArchives
{
    public static class AbpLocalizationOptionsExtensions
    {
        public static void AssemblyResources(this AbpLocalizationOptions options, Assembly assembly)
        {
            Check.NotNull(options, nameof(options));

            var types = assembly.GetTypes().Where(IsModelResources);

            foreach (var type in types)
            {
                IModelResources modelResources = (IModelResources)Activator.CreateInstance(type);
                modelResources.AddVirtualJson(options);
            }
        }

        public static bool IsModelResources(Type type)
        {
            if (!type.IsPublic || type.IsAbstract || type.IsGenericType)
            {
                return false;
            }

            if (typeof(IModelResources).IsAssignableFrom(type))
            {
                return true;
            }

            return false;
        }
    }
}
